package com.simas.common;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

//import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

//import net.rim.device.api.servicebook.ServiceBook;
//import net.rim.device.api.servicebook.ServiceRecord;
import net.rim.device.api.system.CoverageInfo;
//import net.rim.device.api.system.WLANInfo;
import net.rim.device.api.ui.UiApplication;
//import net.rim.device.api.ui.component.Dialog;

import com.simas.screen.XmlHIDScreen;
//import com.simas.screen.XmlMBUScreen;
import com.simas.screen.XmlMBUScreen;
import com.simas.screen.XmlShowINFOScreen;

public class XMLWaitProcess {
	private String url;
	private String method; // GET or POST
	private XmlHIDScreen screen2;
	private XmlMBUScreen screen3;
	private XmlShowINFOScreen screen4;
	private String scrType;
	private CommonConnection comCon = new CommonConnection();
	
//	private ServiceRecord getWAP2ServiceRecord() {
//		ServiceBook sb = ServiceBook.getSB();
//		ServiceRecord[] records = sb.getRecords();
//		for(int i = 0; i < records.length; i++) {
//			String cid = records[i].getCid().toLowerCase();
//			String uid = records[i].getUid().toLowerCase();
//			if (cid.indexOf("wptcp") != -1 &&
//					uid.indexOf("wifi") == -1 &&
//					uid.indexOf("mms") == -1) {
//					return records[i];
//			}
//		}
//		return null;
//	}
	
	public void WaitProcess(String strURLtoGet, String method, XmlHIDScreen screen) {
		this.url = strURLtoGet;
		this.method = method;
		this.screen2 = screen;
		this.scrType = "3";
		GetURL getURL = new GetURL(strURLtoGet);
		PleaseWaitPopupScreen.showScreenAndWait(getURL, "Harap tunggu...");
	}
	
	public void WaitProcess(String strURLtoGet, String method, XmlMBUScreen screen) {
		this.url = strURLtoGet;
		this.method = method;
		this.screen3 = screen;
		this.scrType = "4";
		GetURL getURL = new GetURL(strURLtoGet);
		PleaseWaitPopupScreen.showScreenAndWait(getURL, "Harap tunggu...");
	}
	
	public void WaitProcess(String strURLtoGet, String method, XmlShowINFOScreen screen) {
		this.url = strURLtoGet;
		this.method = method;
		this.screen4 = screen;
		this.scrType = "5";
		GetURL getURL = new GetURL(strURLtoGet);
		PleaseWaitPopupScreen.showScreenAndWait(getURL, "Harap tunggu...");
	}
	
	public class GetURL implements Runnable {
		String strURL = null;
		public GetURL(String strURLtoGet) {
			strURL = strURLtoGet;
		}
		
		public void run() {
			UiApplication.getUiApplication().invokeLater(new Runnable() {
                public void run() {
                	try {
//            			/** tambahan baru **/
//            			String connectionParameters = "";
//            			if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) {
//            				// Connected to a WiFi access point
//            				System.out.println("Connected to a WiFi access point");
//            				connectionParameters = ";interface=wifi";
//            			} else {
//            				int coverageStatus = CoverageInfo.getCoverageStatus();
//            				ServiceRecord record = getWAP2ServiceRecord();
//            				if (record != null && (coverageStatus & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT) {
//            					// Have network coverage and a WAP 2.0 service book record
//            					System.out.println("Have network coverage and a WAP 2.0 service book record");
//            					connectionParameters = ";deviceside=true;ConnectionUID=" + record.getUid();
//            				} else if ((coverageStatus & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS) {
//            					// Have an MDS service book and network coverage
//            					System.out.println("Have an MDS service book and network coverage");
//            					connectionParameters = ";deviceside=false";
//            				} else if ((coverageStatus & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT) {
//            					// Have network coverage but no WAP 2.0 service book record
//            					System.out.println("Have network coverage but no WAP 2.0 service book record");
//            					connectionParameters = ";deviceside=true";
//            				}
//            			}
//            			HttpConnection connection = (HttpConnection)Connector.open(url + connectionParameters);
//            			/** tambahan baru **/
            			
//            			HttpConnection connection = (HttpConnection)Connector.open(url);
                		
//                		System.out.println("url: " + url);
                		HttpConnection connection = comCon.getConnection(url);
            			connection.setRequestMethod(method);
            			int responseCode = connection.getResponseCode();
            			if (responseCode != HttpConnection.HTTP_OK) {
            				if (scrType.equals("1")) {
            					//screen.requestFailed("Unexpected response code: " + responseCode);
            				} else if (scrType.equals("2")) {
            					//screen1.requestFailed("Unexpected response code: " + responseCode);
            				} else if (scrType.equals("3")) {
            					screen2.requestFailed("Unexpected response code: " + responseCode + ", CoverageInfo: " + CoverageInfo.getCoverageStatus());
            				} else if (scrType.equals("4")) {
            					screen3.requestFailed("Unexpected response code: " + responseCode + ", CoverageInfo: " + CoverageInfo.getCoverageStatus());
            				} else if (scrType.equals("5")) {
            					screen4.requestFailed("Unexpected response code: " + responseCode + ", CoverageInfo: " + CoverageInfo.getCoverageStatus());
            				}
            				connection.close();
            				return;
            			}
            			String contentType = connection.getHeaderField("Content-Type");
            			ByteArrayOutputStream baos = new ByteArrayOutputStream();
            			InputStream responseData = connection.openInputStream();
            			byte[] buffer = new byte[1000000];
            			int bytesRead = responseData.read(buffer);
            			while(bytesRead > 0) {
            				baos.write(buffer, 0, bytesRead);
            				bytesRead = responseData.read(buffer);
            			}
            			baos.close();
            			connection.close();
            			if (scrType.equals("1")) {
            				//screen.requestSucceeded(baos.toByteArray(), contentType);
            			} else if (scrType.equals("2")) {
            				//screen1.requestSucceeded(baos.toByteArray(), contentType);
            			} else if (scrType.equals("3")) {
            				screen2.requestSucceeded(baos.toByteArray(), contentType);
            			} else if (scrType.equals("4")) {
            				screen3.requestSucceeded(baos.toByteArray(), contentType);
            			} else if (scrType.equals("5")) {
            				screen4.requestSucceeded(baos.toByteArray(), contentType);
            			}
            		}
            		catch(IOException ex) {
            			if (scrType.equals("1")) {
            				//screen.requestFailed(ex.toString());
            			} else if (scrType.equals("2")) {
            				//screen1.requestFailed(ex.toString());
            			} else if (scrType.equals("3")) {
            				screen2.requestFailed(ex.toString());
            			} else if (scrType.equals("4")) {
            				screen3.requestFailed(ex.toString());
            			} else if (scrType.equals("5")) {
            				screen4.requestFailed(ex.toString());
            			}
            		}
                }
            });
		}
	}
}
